package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    // 新增菜品 +口味
    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO){
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        //向菜品表插入一条数据
        dishMapper.savedish(dish);
        //获取生成的菜品id，这个id通过xml文件中获取
        Long dishId = dish.getId();
        //向口味表插入多条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishMapper.saveFlavor(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO){
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> pageQuery=dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(pageQuery.getTotal(),pageQuery.getResult());
    }

    @Override
    public void deleteBatch(List<Long> ids){
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            //判断菜品是否起售
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException("起售中的菜品无法删除");
            }
        }
            //判断菜品是否关联套餐
            List<Long> setmealIds=setmealDishMapper.getSetmealIdsByDishId(ids);
            if(setmealIds!=null&&setmealIds.size()>0){
                throw new DeletionNotAllowedException("菜品正在被套餐关联，无法删除");
            }
            for (Long id : ids) {
                dishMapper.deletedish(id);
                dishMapper.deletedishFlavor(id);
            }
    }

    @Override
    public void startOrStop(Integer status, Long id){
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateqt(dish);
    }

    @Override
    public void update(DishDTO dishDTO){
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        //更新菜品表
        dishMapper.update(dish);
        //删除原有口味表
        dishMapper.deletedishFlavor(dishDTO.getId());
        //添加新的口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();//获取口味信息集合
        if(flavors != null && flavors.size() > 0){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishMapper.saveFlavor(flavors);
        }
    }

    @Override
    public DishVO getById(Long id){
        //查询菜品基本信息
        Dish dish = dishMapper.getById(id);
        //查询菜品口味信
        List<DishFlavor> flavors = dishMapper.getByDishId(id);
        //封装
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public List<Dish> list(Long categoryId){
        List<Dish> list = dishMapper.list(categoryId);
        return list;
    }


    // 根据条件查询菜品数据,用户端
    @Override
    public List<DishVO> listWithFlavor(Dish dish){
        //查询菜品数据,不包含口味
        List<Dish> dishList = dishMapper.list1(dish);
        //创建一个DishVO类型的集合，用于存储查询结果
        List<DishVO> dishVOList = new ArrayList<>();
        //遍历dishList集合，将Dish对象转换为DishVO对象，并添加到dishVOList集合中
        //包含口味信息
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);//将d对象中的属性复制到dishVO对象中

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);//将dishVO添加到dishVOList中
        }

        return dishVOList;

    }
}
